<!DOCTYPE html>
<html>

<head lang="en">
	<meta charset="UTF-8">
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
	<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no,width=device-width,height=device-height" />
	<title>志愿者打卡器</title>
	<link rel="stylesheet" href="dist/css/app.css">
	<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
	<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
	<script src="//cdn.bootcss.com/jszip/3.0.0/jszip.min.js"></script>
</head>

<body>
	<div class="pvalidate">
		<h2>请输入活动邀请码</h2>
		<input type="text" id="yaoqingma" ng-model="validCode" />
		<p>没有邀请码，请联系您的志愿组织管理员</p>
		<div class="bot-title"><a class="next">下一步</a></div>
	</div>
</body>
<script src="libs/qrcode/qrcode.min.js"></script>
<script type="text/javascript">
(function($) {
	$(function() {

		function IdCard(UUserCard,num){
			if(num==1){
				//获取出生日期
				birth=UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
				return birth;
			}
			if(num==2){
				//获取性别
				if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
					//男
					return "男";
				} else {
					//女
					return "女";
				}
			}
			if(num==3){
				//获取年龄
				var myDate = new Date();
				var month = myDate.getMonth() + 1;
				var day = myDate.getDate();
				var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
				if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
					age++;
				}
				return age;
			}
		}


		//获取url中的参数
		function GetQueryString(name) { //截取字符串？ &
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) {
				return unescape(r[2]);
			} else {
				return null;
			}
		}
		var memberId = GetQueryString("memberId");
		var memb1er ="";
		var isInsurance = "";
		$.ajax({
			url: "http://pingan2016.dakaqi.cn/spreadInsurance/checkUser",
//			url: "http://localhost:8080/spreadInsurance/checkUser",
			type: 'post',
			dataType: 'json',
			contentType: 'application/json; charset=utf-8',
			data: JSON.stringify({
				memberId: memberId
			}),
			error: function() {
				alert('error')
			}, //错误执行方法
			success: function(data) {
					if (data.code == 0) {
						member = data.data.member;
						isInsurance = data.data.havingInsurance
						window.sessionStorage.setItem("member", JSON.stringify(data.data.member));
					} else {
						alert(data.message);
					}
				} //成功执行方法
		})
		$(".next").click(function() {
			if(isInsurance){
				alert('对不起, 您近期已经领取过保险');
				return
			}

			if(member == "" || member.cardNO == ""){
				alert("请完善个人信息");
				return;
			}

			var age = IdCard(member.cardNO,3);

			if(age<18 || age > 45){
				alert("本次活动仅限18-45周岁");
				return;
			}

			var yaoqingma = $("#yaoqingma").val();
			if(yaoqingma != ""){

				$.ajax({
					url: "http://pingan2016.dakaqi.cn/spreadInsurance/findInviteCode",
//					url: "http://localhost:8080/spreadInsurance/findInviteCode",
					type: 'post',
					dataType: 'json',
					contentType: 'application/json; charset=utf-8',
					data: JSON.stringify({
						inviteCode: yaoqingma
					}),
					error: function() {
						alert('error')
					}, //错误执行方法
					success: function(data) {
						if (data.code != 0) {
							alert(data.message);
							return;
						}else{
							window.sessionStorage.setItem("yaoqingma", yaoqingma);
							window.location.href = "/static/setvalidate.html";
						}
					} //成功执行方法
				})

			}else{
				alert("请输入邀请码");
			}

		})
	})
})(jQuery)


</script>

</html>
